home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / Macintosh Tracker 1.20 / source / Server⁄Tracker 4.0 / mac_hack.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-23  |  11.0 KB  |  551 lines  |  [TEXT/KAHL]

  1. /* mac_hack.c */
  2.  
  3. /* All the stuff in this file was written by Thomas R. Lawrence. */
  4. /* See the "mac_readme" or "mac_programmer_info" files for more information */
  5. /* about the Macintosh port */
  6.  
  7. #include "mac_hack.h"
  8. #include "mac_event.h"
  9. #include <console.h>
  10. #include <unix.h>
  11.  
  12. #include <Aliases.h>
  13. #include <SANE.h>
  14. #include <GestaltEqu.h>
  15. #include <Power.h>
  16.  
  17.  
  18. /* stuff from mac_event.c */
  19. extern FSSpec                        GlobalFileSpec;
  20. extern char                            FakeKeyBuffer[MAXKEYS];
  21. extern int                            KeyBufPtr;
  22. extern Boolean                    QuitPending;
  23. extern short                        Pausing;
  24. extern Boolean                    ReceivedOpenEventFlag;
  25.  
  26. extern short                        AntiAliasing;
  27. extern short                        StereoOn;
  28. extern unsigned short        SamplingRate;
  29. extern short                        NumRepeats;
  30. extern short                        Speed;
  31. extern short                        StereoMix;
  32. extern short                        Loudness;
  33.  
  34.  
  35. #define VOLUMEINCREMENT (8)
  36.  
  37. #define SMALL_MAX_TIME_SLICE (10)
  38. #define LARGE_MAX_TIME_SLICE (120)
  39. #define WHATISBREATHINGSPACE (48) /* about 16 seconds at 22254 Hz */
  40.  
  41. long                                        PowerManagerInfo = 0;
  42.  
  43.  
  44. void            ReenableIdleState(void)
  45.     {
  46.         if ((PowerManagerInfo & (1 << gestaltPMgrExists)) != 0)
  47.             {
  48.                 EnableIdle();
  49.             }
  50.     }
  51.  
  52.  
  53. #define MAXCOUNT (3) /* every MAXCOUNTth call to may_getchar actually does something */
  54.  
  55. static unsigned long        LastEventTime = 0;
  56. static short                        Count = MAXCOUNT;
  57.  
  58. /* this is called by the tracker code to get keypresses from the user. */
  59. /* we use it to manage use of processor. */
  60. int        may_getchar(void)
  61.   {
  62.      LoopPoint:
  63.         if (QuitPending)
  64.             {
  65.                 end_all("");
  66.             }
  67.  
  68.         if (Pausing)
  69.             {
  70.                 goto DoThingPoint;
  71.             }
  72.  
  73.         if (Count < 0)
  74.             {
  75.                 Count = MAXCOUNT;
  76.             }
  77.          else
  78.             {
  79.                 Count -= 1;
  80.                 return EOF;
  81.             }
  82.  
  83.          if (NumberPendingBlocks() < WHATISBREATHINGSPACE)
  84.              {
  85.                  /* if we don't have very many blocks prepared, then we don't call */
  86.                  /* the event routine for a long time.  In other words, we give */
  87.                  /* ourselves a LARGE_MAX_TIME_SLICE */
  88.                  if (TickCount() - LastEventTime > LARGE_MAX_TIME_SLICE)
  89.                      {
  90.                       DoThingPoint:
  91.                          WaitForEvent(0);
  92.                          LastEventTime = TickCount();
  93.                      }
  94.              }
  95.          else
  96.             {
  97.                 /* otherwise, we grant ourselves a SMALL_MAX_TIME_SLICE during */
  98.                 /* which to construct more blocks */
  99.                  if (TickCount() - LastEventTime > SMALL_MAX_TIME_SLICE)
  100.                      {
  101.                          goto DoThingPoint;
  102.                      }
  103.             }
  104.  
  105.         /* extract 'keypresses' from our buffer.  First we handle our own */
  106.         /* special volume controls and our special pause function, and if the */
  107.         /* key wasn't one of those, we pass it back to the caller to be handled. */
  108.         if (KeyBufPtr > 0)
  109.             {
  110.                 int                    Scan;
  111.                 int                    KeyTemp;
  112.  
  113.                 KeyTemp = (unsigned char)FakeKeyBuffer[0];
  114.                 for (Scan = 1; Scan < KeyBufPtr; Scan += 1)
  115.                     {
  116.                         FakeKeyBuffer[Scan - 1] = FakeKeyBuffer[Scan];
  117.                     }
  118.                 KeyBufPtr -= 1;
  119.                 if (KeyTemp == '+')
  120.                     {
  121.             Loudness += VOLUMEINCREMENT;
  122.             if (Loudness > 255)
  123.               {
  124.                 Loudness = 255;
  125.               }
  126.             ResetVolumeTable();
  127.             goto LoopPoint;
  128.           }
  129.         if (KeyTemp == '-')
  130.             {
  131.             Loudness -= VOLUMEINCREMENT;
  132.             if (Loudness < 0)
  133.               {
  134.                 Loudness = 0;
  135.               }
  136.             ResetVolumeTable();
  137.             goto LoopPoint;
  138.           }
  139.         if (KeyTemp == ' ')
  140.             {
  141.                 TogglePause();
  142.                 goto LoopPoint;
  143.             }
  144.         if (KeyTemp == '<')
  145.             {
  146.                 /* fudge rewind into restart, for consistency */
  147.                 KeyTemp = 'r';
  148.             }
  149.         if ((KeyTemp == '>') || (KeyTemp == '|'))
  150.             {
  151.                 /* these don't work well in this version because it doesn't run */
  152.                 /* at interrupt level, but instead precomputes the song as far */
  153.                 /* ahead as possible.  This usually means, on a fast computer, that */
  154.                 /* you don't hear the result of clicking the button until 5 or */
  155.                 /* 10 seconds later.  So I just block them for the time being.  If */
  156.                 /* I or someone else hacks this to run at interrupt time, then */
  157.                 /* we can put these back in. */
  158.                 goto LoopPoint;
  159.             }
  160.                 return KeyTemp;
  161.             }
  162.          else
  163.              {
  164.                  return EOF;
  165.              }
  166.   }
  167.  
  168.  
  169. /* this is the real main function.  We basically wait for the open event, */
  170. /* then using the parameters sent to use, we construct a fake little command line */
  171. /* string which is passed to the tracker's main function.  It never knows */
  172. /* what happened. */
  173. #undef main
  174. void        main(void)
  175.   {
  176.     int                            argc;
  177.     char*                        argv[14];
  178.     OSErr                        Error;
  179.     long                        ProcTypeInfo;
  180.     char                        SamplingRateString[20];
  181.     char                        RepeatsString[20];
  182.     char                        SpeedString[20];
  183.     char                        MixString[20];
  184.  
  185.         MaxApplZone();
  186.         InitGraf(&thePort);
  187.         InitFonts();
  188.         FlushEvents(everyEvent,0);
  189.         InitWindows();
  190.         InitMenus();
  191.         TEInit();
  192.         InitDialogs(NULL);
  193.         InitCursor();
  194.  
  195.         if (!RegisterEventHandlers())
  196.             {
  197.                 return;
  198.             }
  199.  
  200.         Error = Gestalt(gestaltPowerMgrAttr,&PowerManagerInfo);
  201.         if (Error != noErr)
  202.             {
  203.                 return;
  204.             }
  205.         if ((PowerManagerInfo & (1 << gestaltPMgrExists)) != 0)
  206.             {
  207.                 DisableIdle();
  208.                 atexit(&ReenableIdleState);
  209.             }
  210. #if __option(mc68020)
  211.         Error = Gestalt(gestaltProcessorType,&ProcTypeInfo);
  212.         if ((Error != noErr) || (ProcTypeInfo == gestalt68000))
  213.             {
  214.                 FatalError(FatalError68020NeededID);
  215.                 return;
  216.             }
  217. #endif
  218.  
  219. #ifdef AskForFile
  220.         {
  221.             Point                                Thing = {50,50};
  222.             StandardFileReply        R;
  223.  
  224.             /* this is only used for debugging when we don't have access to the interface */
  225.             /* program (since the interface program can't link to this program when it */
  226.             /* is being run under the debugger.) */
  227.             StandardGetFile(NULL,-1,NULL,&R);
  228.             GlobalFileSpec = R.sfFile;
  229.         }
  230. #else
  231.         /* waiting for open document command to come */
  232.         while (!ReceivedOpenEventFlag && !QuitPending)
  233.             {
  234.                 WaitForEvent(60);
  235.             }
  236.         if (QuitPending)
  237.             {
  238.                 return;
  239.             }
  240. #endif
  241.  
  242.         /* constructing parameter list */
  243.         argc = 14;
  244.         argv[0] = "tracker";
  245.         argv[1] = "-oversample";
  246.         if (AntiAliasing)
  247.             {
  248.                 argv[2] = "2";
  249.             }
  250.          else
  251.             {
  252.                 argv[2] = "1";
  253.             }
  254.         if (StereoOn)
  255.             {
  256.                 argv[3] = "-stereo";
  257.             }
  258.          else
  259.             {
  260.                 argv[3] = "-mono";
  261.             }
  262.         argv[4] = "-frequency";
  263.         argv[5] = SamplingRateString; sprintf(SamplingRateString,"%d",(int)SamplingRate);
  264.         argv[6] = "-repeats";
  265.         argv[7] = RepeatsString; sprintf(RepeatsString,"%d",(int)NumRepeats);
  266.         argv[8] = "-speed";
  267.         argv[9] = SpeedString; sprintf(SpeedString,"%d",(int)Speed);
  268.         argv[10] = "-mix";
  269.         argv[11] = MixString; sprintf(MixString,"%d",(int)StereoMix);
  270.         argv[12] = "-tolerant";
  271.         argv[13] = "FileName";
  272.  
  273.     main2(argc,argv);
  274.   }
  275.  
  276.  
  277. void* popen(char* pipe, char* Mode)
  278.   {
  279.     perror("Macintosh doesn't have pipes!");
  280.     FatalError(FatalErrorCantOpenCompressedFiles);
  281.     end_all("");
  282.   }
  283.  
  284.  
  285. void pclose(FILE* file)
  286.   {
  287.   }
  288.  
  289.  
  290.  
  291.  
  292. #include "defs.h"
  293. #include "extern.h"
  294. #include "song.h"
  295. #include "channel.h"
  296.  
  297. #include "tags.h"
  298.  
  299.  
  300. int show;  /* prototype in extern.h */
  301.  
  302.  
  303.  
  304.  
  305. int MYprintf(...)
  306.     {
  307.     }
  308.  
  309. int MYfprintf(...)
  310.     {
  311.     }
  312.  
  313. int MYfgetc(FILE* FileToGetFrom)
  314.     {
  315.         unsigned char            Temp;
  316.         long                            Length;
  317.         OSErr                            Error;
  318.  
  319.         if (FileToGetFrom->BufPtr == FILEBUFFERSIZE)
  320.             {
  321.                 FileToGetFrom->BufPtr = 0;
  322.                 Length = FILEBUFFERSIZE;
  323.                 SetFPos(FileToGetFrom->MacFileHandle,fsFromStart,FileToGetFrom->Index);
  324.                 Error = FSRead(FileToGetFrom->MacFileHandle,&Length,
  325.                     &(FileToGetFrom->Buffer[0]));
  326.             }
  327.         Temp = FileToGetFrom->Buffer[FileToGetFrom->BufPtr];
  328.         FileToGetFrom->Index += 1;
  329.         FileToGetFrom->BufPtr += 1;
  330.         if (FileToGetFrom->Index >= FileToGetFrom->EndOfFile)
  331.             {
  332.                 return EOF;
  333.             }
  334.          else
  335.             {
  336.                 return Temp;
  337.             }
  338.     }
  339.  
  340. int MYfputc(int CharToPut, FILE *FileToPutTo)
  341.     {
  342.     }
  343.  
  344. FILE* MYfopen(char* FileName, char* Mode)
  345.     {
  346.         OSErr                Error;
  347.         FILE*                Temp;
  348.         short                FileHandleTemp;
  349.  
  350.         Error = FSpOpenDF(&GlobalFileSpec,fsCurPerm,&FileHandleTemp);
  351.         if (Error != noErr)
  352.             {
  353.                 BUG("\pError opening file in MYfopen");
  354.                 FatalError(FatalErrorCouldntOpenFile);
  355.                 end_all("");
  356.                 /* return NULL; */
  357.             }
  358.          else
  359.             {
  360.                 Temp = (FILE*)NewPtr(sizeof(FILE));
  361.                 Temp->MacFileHandle = FileHandleTemp;
  362.                 Temp->Index = 0;
  363.                 GetEOF(FileHandleTemp,&(Temp->EndOfFile));
  364.                 Temp->BufPtr = FILEBUFFERSIZE; /* force refill upon first read */
  365.                 return Temp;
  366.             }
  367.     }
  368.  
  369. int MYfclose(FILE* FileToClose)
  370.     {
  371.         OSErr                Error;
  372.  
  373.         if (FileToClose != NULL)
  374.             {
  375.                 Error = FSClose(FileToClose->MacFileHandle);
  376.                 if (Error != noErr)
  377.                     {
  378.                         FatalError(FatalErrorCouldntCloseFile);
  379.                         BUG("\pError closing file in MYfclose");
  380.                     }
  381.                 DisposPtr((Ptr)FileToClose);
  382.             }
  383.     }
  384.  
  385. int MYfread(char* PlaceToPut, int SizeOfElement, int NumElements, FILE* TheFile)
  386.     {
  387.         long        NumBytesDone;
  388.         OSErr        LastError;
  389.         long        NumberOfBytes;
  390.         long        LengthTemp;
  391.  
  392.         SetFPos(TheFile->MacFileHandle,fsFromStart,TheFile->Index);
  393.         NumberOfBytes = SizeOfElement * NumElements;
  394.         TheFile->Index += NumberOfBytes;
  395.      LoopPoint:
  396.         NumBytesDone = NumberOfBytes;
  397.         LastError = FSRead(TheFile->MacFileHandle,&NumBytesDone,PlaceToPut);
  398.         PlaceToPut += NumBytesDone;
  399.         NumberOfBytes -= NumBytesDone;
  400.         if ((LastError == noErr) && (NumberOfBytes != 0)) goto LoopPoint;
  401.         if (LastError != noErr)
  402.             {
  403.                 BUG("\pError reading from file in MYfread");
  404.             }
  405.         TheFile->BufPtr = TheFile->Index & (FILEBUFFERSIZE - 1);
  406.         SetFPos(TheFile->MacFileHandle,fsFromStart,TheFile->Index - TheFile->BufPtr);
  407.         LengthTemp = FILEBUFFERSIZE;
  408.         FSRead(TheFile->MacFileHandle,&LengthTemp,&(TheFile->Buffer[0]));
  409.         return LastError;
  410.     }
  411.  
  412. void* MYmalloc(long SizeOfBlock)
  413.     {
  414.         void*            Block;
  415.  
  416.         Block = NewPtrClear(SizeOfBlock);
  417.         if (Block == NULL)
  418.             {
  419.                 FatalError(FatalErrorOutOfMemory);
  420.                 end_all("");
  421.             }
  422.         return Block;
  423.     }
  424.  
  425. void MYfree(void* Block)
  426.     {
  427.         DisposPtr(Block);
  428.     }
  429.  
  430. void *MYcalloc(size_t NumThings, size_t SizeOfThing)
  431.   {
  432.       return malloc((NumThings + 1) * SizeOfThing); /* extra for antialiasing byte */
  433.   }
  434.  
  435. #undef exit
  436. void MYexit(int Value)
  437.     {
  438.         /* ExitToShell(); */
  439.         exit(Value); /* we DO want to call "atexit"ed things, such as powerbook idle fixer */
  440.     }
  441.  
  442. char* MYgetenv(char* MeaninglessParameter)
  443.     {
  444.         return NULL;
  445.     }
  446.  
  447. void MYperror(char* ErrorMessage)
  448.     {
  449.     }
  450.  
  451. void MYputs(char* Message)
  452.     {
  453.     }
  454.  
  455. long double MYfloor(long double Base)
  456.     {
  457.         return (long int)Base;
  458.     }
  459.  
  460. long double MYpow(long double Base, long double Exponent)
  461.     {
  462.         extended            ExtBase,ExtExponent,ExtResult;
  463.         long double        DblResult;
  464.  
  465.         x96tox80(&Base,&ExtBase);
  466.         x96tox80(&Exponent,&ExtExponent);
  467.         ExtResult = power(ExtBase,ExtExponent);
  468.         x80tox96(&ExtResult,&DblResult);
  469.         return DblResult;
  470.     }
  471.  
  472.  
  473.  
  474.  
  475.  
  476. GENERIC        begin_info(char *title) /* declared as GENERIC begin_info() */
  477.     {
  478.         return 0;
  479.     }
  480.  
  481.  
  482. void            display_pattern(int current, int total)
  483.     {
  484.     }
  485.  
  486.  
  487. void            end_info(GENERIC handle)
  488.     {
  489.     }
  490.  
  491.  
  492. int       run_in_fg(void)  /* prototype in mac_hack.h is BOOL run_in_fg(void) */
  493.   {
  494.     return TRUE;
  495.   }
  496.  
  497.  
  498. void      sane_tty(void)
  499.   {
  500.   }
  501.  
  502.  
  503. void      nonblocking_io(void)
  504.   {
  505.   }
  506.  
  507.  
  508. LOCAL struct tag end_marker;
  509.  
  510. struct tag *get_ui()
  511.     {
  512.         may_getchar();
  513.         end_marker.type = TAG_END;
  514.         return &end_marker;
  515.     }
  516.  
  517.  
  518. void info(GENERIC handle, char *line)
  519.     {
  520.     }
  521.  
  522.  
  523. void infos(GENERIC handle, char *s)
  524.     {
  525.     }
  526.  
  527.  
  528. void notice(char *s)
  529.     {
  530.     }
  531.  
  532.  
  533. char *new_scroll(void)
  534.     {
  535.     }
  536.  
  537.  
  538. void song_title(char *s)
  539.     {
  540.     }
  541.  
  542.  
  543. void scroll()
  544.     {
  545.     }
  546.  
  547.  
  548. void status(char *s)
  549.     {
  550.     }
  551.